package QuanLyLoaiThuoc;

import KetNoiSQL.KetNoiSQL;
import KiemTra.KiemTra;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class DanhSachLoaiThuoc extends javax.swing.JPanel {

    private DefaultTableModel modelLoaiThuoc = new DefaultTableModel();
    String tenBang = "LoaiThuoc";
    Vector vLay = new Vector();

    public DanhSachLoaiThuoc() {
        initComponents();
        txtMaLoaiThuoc.setEnabled(false);
        try {
            vLay = LoaiThuoc.xemLoaiThuoc();
            this.taiBangLoaiThuoc(tenBang, vLay);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    //Tải dữ liệu vào bảng Loại thuốc

    public void taiBangLoaiThuoc(String tenBang, Vector vLay) throws SQLException {
        modelLoaiThuoc.setRowCount(0);
        Vector cotLoaiThuoc = new Vector();
        cotLoaiThuoc.addElement("Mã loại thuốc");
        cotLoaiThuoc.addElement("Tên loại thuốc");
        modelLoaiThuoc.setColumnIdentifiers(cotLoaiThuoc);
        LoaiThuoc lt = new LoaiThuoc();
        for (int i = 0; i < vLay.size(); i++) {
            lt = (LoaiThuoc) vLay.get(i);
            Vector hangLoaiThuoc = new Vector();
            hangLoaiThuoc.addElement(lt.getMaLoaiThuoc());
            hangLoaiThuoc.addElement(lt.getTenLoaiThuoc());
            modelLoaiThuoc.addRow(hangLoaiThuoc);
        }
        tbLoaiThuoc.setModel(modelLoaiThuoc);
    }
    //Kiểm tra loại thuốc

    public boolean ktraLoaiThuoc() {
        if (txtTenLoaiThuoc.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Bạn chưa nhập tên loại thuốc", "Thông báo", JOptionPane.ERROR_MESSAGE);
            txtTenLoaiThuoc.requestFocus();
            return false;
        }
        return true;
    }
    //Kiểm tra tên Loại thuốc

    public boolean ktraTenLoaiThuoc() {
        String ten = txtTenLoaiThuoc.getText().trim();
        boolean kt = true;
        try {
            ResultSet rs = KetNoiSQL.thucHienTruVan("spXemTenLoaiThuoc");
            while (rs.next()) {
                if (ten.equals(rs.getString("tenLoaiThuoc"))) {
                    JOptionPane.showMessageDialog(this, "Tên loại thuốc đã tồn tại.Nhập lại!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
//                    txtTenLoaiThuoc.setText("");
                    txtTenLoaiThuoc.requestFocus();
                    kt = false;
                    break;
                }
            }
            
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return kt;
    }

    //Thêm loại thuốc mới
    public void themLoaiThuoc() {

        if (!ktraLoaiThuoc()) {
            return;
        }
       if(!ktraTenLoaiThuoc()){
           return;
       }
        int kt = 0;
        LoaiThuoc lt = new LoaiThuoc();
        lt.setTenLoaiThuoc(txtTenLoaiThuoc.getText().trim());
        try {
            kt = LoaiThuoc.themLoaiThuoc(lt);
            if (kt == 1) {
                JOptionPane.showMessageDialog(this, "Thêm mới loại thuốc thành công !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                vLay = LoaiThuoc.xemLoaiThuoc();
                this.taiBangLoaiThuoc(tenBang, vLay);
            } else {
                JOptionPane.showMessageDialog(this, "Thêm mới loại thuốc không thành công.Xem lại", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    //Cập nhật loại thuốc
    public void capNhatLoaiThuoc() {
        int kt = 0;
        if (!ktraLoaiThuoc()) {
            return;
        }
        LoaiThuoc lt = new LoaiThuoc();
        int maLoaiThuoc = Integer.parseInt(txtMaLoaiThuoc.getText().trim());
        lt.setMaLoaiThuoc(maLoaiThuoc);
        lt.setTenLoaiThuoc(txtTenLoaiThuoc.getText().trim());
        try {
            kt = LoaiThuoc.capNhatLoaiThuoc(lt);
            if (kt == 1) {
                JOptionPane.showMessageDialog(this, "Cập nhật thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                vLay = LoaiThuoc.xemLoaiThuoc();
                this.taiBangLoaiThuoc(tenBang, vLay);
            } else {
                JOptionPane.showMessageDialog(this, "Cập nhật bị lỗi.Xem lại!", "Thông báo", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    //Xóa loại thuốc

    public void xoaLoaiThuoc() {
        int show = JOptionPane.showConfirmDialog(this, "Bạn có thực sự muốn xóa loại thuốc ?", "Thông báo xóa", JOptionPane.YES_NO_OPTION);
        if (show == JOptionPane.YES_OPTION) {
            int kt = 0, dem = 0;
            int[] rArr = tbLoaiThuoc.getSelectedRows();
            try {
                for (int i = 0; i < rArr.length; i++) {
                    kt = LoaiThuoc.xoaLoaiThuoc(tbLoaiThuoc.getValueAt(rArr[i], 1).toString());
                    if (kt == 1) {
                        dem++;
                    }
                }
                if (dem > 0) {
                    JOptionPane.showMessageDialog(this, "Xóa " + dem + " loai thuốc thành công", "Thông báo xóa", JOptionPane.INFORMATION_MESSAGE);
                    vLay = LoaiThuoc.xemLoaiThuoc();
                    this.taiBangLoaiThuoc(tenBang, vLay);
                } else {
                    JOptionPane.showMessageDialog(this, "Xóa loại thuốc bị lỗi  !", "Thông báo xóa", JOptionPane.ERROR_MESSAGE);
                }
            } catch (SQLException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }
    // trở lại

    public void resetForm() {
        txtMaLoaiThuoc.setText("");
        txtTenLoaiThuoc.setText("");
        txtTenLoaiThuoc.requestFocus();
    }
    //Kiểm tra tìm kiếm

    public boolean ktTimKiemLoaiThuoc() {
        KiemTra kt = new KiemTra();
        if (txtTenLoaiThuoc.getText().contains("'")) {
            JOptionPane.showMessageDialog(this, "Tên loại thuốc không hợp lệ!", "Tìm kiếm", JOptionPane.ERROR_MESSAGE);
            txtTenLoaiThuoc.setText("");
            txtTenLoaiThuoc.requestFocus();
            return false;
        }
        return true;
    }
    //Tìm kiếm nhà cung cấp

    public void timKiemLoaiThuoc() throws SQLException {
        if (!this.ktTimKiemLoaiThuoc()) {
            return;
        }
        Vector vlist = new Vector();
        if (txtTenLoaiThuoc.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Bạn phải nhập vào tên cần tìm kiếm", "Thông Báo", JOptionPane.OK_OPTION);
            vLay = LoaiThuoc.xemLoaiThuoc();
            this.taiBangLoaiThuoc(tenBang, vLay);
        } else {
            vlist.addElement(txtTenLoaiThuoc.getText().trim());
            vLay = LoaiThuoc.timKiemTenLoaiThuoc(vlist);
            this.taiBangLoaiThuoc(tenBang, vLay);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbLoaiThuoc = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        txtMaLoaiThuoc = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTenLoaiThuoc = new javax.swing.JTextField();
        btTimKiem = new javax.swing.JButton();
        btHuy = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btThem = new javax.swing.JButton();
        btXoa = new javax.swing.JButton();
        btTroLai = new javax.swing.JButton();
        btCapNhat = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách loại thuốc"));

        tbLoaiThuoc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbLoaiThuoc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbLoaiThuocMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbLoaiThuoc);

        jLabel2.setText("Loại thuốc ID");

        jLabel3.setText("Tên loại thuốc");

        btTimKiem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Search.png"))); // NOI18N
        btTimKiem.setText("Tìm kiếm");
        btTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTimKiemActionPerformed(evt);
            }
        });

        btHuy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/Delete.png"))); // NOI18N
        btHuy.setText("Hủy");
        btHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btHuyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtMaLoaiThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtTenLoaiThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(btTimKiem)
                        .addGap(18, 18, 18)
                        .addComponent(btHuy, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btTimKiem)
                    .addComponent(txtTenLoaiThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtMaLoaiThuoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(btHuy))
                .addGap(31, 31, 31))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btHuy, btTimKiem, jLabel2, jLabel3, txtMaLoaiThuoc, txtTenLoaiThuoc});

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/icons_pills.png"))); // NOI18N

        btThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/add.png"))); // NOI18N
        btThem.setText("Thêm");
        btThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemActionPerformed(evt);
            }
        });

        btXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/cross.png"))); // NOI18N
        btXoa.setText("Xóa");
        btXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaActionPerformed(evt);
            }
        });

        btTroLai.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/reset.png"))); // NOI18N
        btTroLai.setText("Làm lại");
        btTroLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTroLaiActionPerformed(evt);
            }
        });

        btCapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Anh/edit.png"))); // NOI18N
        btCapNhat.setText("Cập nhật");
        btCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addGap(100, 100, 100))
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btCapNhat)
                .addGap(91, 91, 91)
                .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(btTroLai, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(198, 198, 198)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btThem, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btTroLai, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 74, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemActionPerformed
        // TODO add your handling code here:
        this.themLoaiThuoc();
    }//GEN-LAST:event_btThemActionPerformed

    private void btCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatActionPerformed
        // TODO add your handling code here:
        this.capNhatLoaiThuoc();
    }//GEN-LAST:event_btCapNhatActionPerformed

    private void btXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaActionPerformed
        // TODO add your handling code here:
        this.xoaLoaiThuoc();
    }//GEN-LAST:event_btXoaActionPerformed

    private void btTroLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTroLaiActionPerformed
        // TODO add your handling code here:
        this.resetForm();
    }//GEN-LAST:event_btTroLaiActionPerformed

    private void tbLoaiThuocMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLoaiThuocMouseClicked
        // TODO add your handling code here:
        int r = tbLoaiThuoc.getSelectedRow();
        if (r < 0) {
            return;
        }
        txtMaLoaiThuoc.setText("" + tbLoaiThuoc.getValueAt(r, 0));
        txtTenLoaiThuoc.setText("" + tbLoaiThuoc.getValueAt(r, 1));
    }//GEN-LAST:event_tbLoaiThuocMouseClicked

    private void btTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTimKiemActionPerformed
        try {
            this.timKiemLoaiThuoc();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btTimKiemActionPerformed

    private void btHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btHuyActionPerformed
        txtTenLoaiThuoc.setText("");
        try {
            vLay = LoaiThuoc.xemLoaiThuoc();
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachLoaiThuoc.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            this.taiBangLoaiThuoc(tenBang, vLay);
        } catch (SQLException ex) {
            Logger.getLogger(DanhSachLoaiThuoc.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btHuyActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCapNhat;
    private javax.swing.JButton btHuy;
    private javax.swing.JButton btThem;
    private javax.swing.JButton btTimKiem;
    private javax.swing.JButton btTroLai;
    private javax.swing.JButton btXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbLoaiThuoc;
    private javax.swing.JTextField txtMaLoaiThuoc;
    private javax.swing.JTextField txtTenLoaiThuoc;
    // End of variables declaration//GEN-END:variables
}
